package com.ysqcould.message.utils.wxmessage;

import com.ysqcould.allocation.domain.MessageConfig;
import com.ysqcould.allocation.domain.WxUser;
import com.ysqcould.allocation.mapper.MessageConfigMapper;
import com.ysqcould.allocation.mapper.WxUserMapper;
import com.ysqcould.common.core.domain.AjaxResult;
import com.ysqcould.common.utils.spring.SpringUtils;
import com.ysqcould.message.domain.WxMessage;
import com.ysqcould.message.domain.WxMessagePress;
import com.ysqcould.message.mapper.WxMessageMapper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 模板消息发送工具类
 * @author jcd
 */
public class MessageUtil {

    static MessageConfigMapper messageConfigMapper = SpringUtils.getBean(MessageConfigMapper.class);
    static WxMessageMapper messageMapper = SpringUtils.getBean(WxMessageMapper.class);
    static WxUserMapper userMapper = SpringUtils.getBean(WxUserMapper.class);
    static ExecutorService singleThreadExecutorScreen = Executors.newSingleThreadExecutor();

    /**
     * 发送微信模板消息
     * @param userId 协会id
     * @param id 消息id
     * @param typeId 消息模板id
     * @return
     */
    public  static AjaxResult sendWxMessage(Long userId, Long id, Long typeId) {

        final MessageConfig messageConfig = messageConfigMapper.getMessageConfig(userId, typeId);

        final WxMessage message = messageMapper.selectWxMessageById(id);
        String str = null;
        if(messageConfig.getType()==1) {
            str = WxTemplate.getDataWxMessage(
                    message.getFirstData(), message.getFirstColor(),
                    message.getKeyword1Data(), message.getKeyword1Color(),
                    message.getKeyword2Data(), message.getKeyword2Color(),
                    message.getRemark(), message.getRemarkColor());

        }else if(messageConfig.getType()==3){
            str = WxTemplate.getDataWxMessage(
                    message.getFirstData(), message.getFirstColor(),
                    message.getKeyword1Data(), message.getKeyword1Color(),
                    message.getKeyword2Data(), message.getKeyword2Color(),
                    message.getKeyword3Data(), message.getKeyword3Color(),
                    message.getKeyword4Data(), message.getKeyword4Color(),
                    message.getRemark(), message.getRemarkColor());

        }else if(messageConfig.getType()==4) {
            str = WxTemplate.getDataWxMessage(
                    message.getFirstData(), message.getFirstColor(),
                    message.getKeyword1Data(), message.getKeyword1Color(),
                    message.getKeyword2Data(), message.getKeyword2Color(),
                    message.getKeyword3Data(), message.getKeyword3Color(),
                    message.getRemark(), message.getRemarkColor());

        }else if(messageConfig.getType()==5){
            str = WxTemplate.getDataWxMessage(
                    message.getFirstData(), message.getFirstColor(),
                    message.getKeyword1Data(), message.getKeyword1Color(),
                    message.getKeyword2Data(), message.getKeyword2Color(),
                    message.getKeyword3Data(), message.getKeyword3Color(),
                    message.getKeyword4Data(), message.getKeyword4Color(),
                    message.getRemark(), message.getRemarkColor());

        }else if(messageConfig.getType()==6){
            str = WxTemplate.getDataSignUpWxMessage(
                    message.getFirstData(), message.getFirstColor(),
                    message.getKeyword1Data(), message.getKeyword1Color(),
                    message.getKeyword2Data(), message.getKeyword2Color(),
                    message.getKeyword3Data(), message.getKeyword3Color(),
                    message.getKeyword4Data(), message.getKeyword4Color(),
                    message.getRemark(), message.getRemarkColor());

        }
        String data = str;
        String[] userIds = message.getOpenids().split(";");
        String url = message.getLinkUrl();

        //创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
        singleThreadExecutorScreen.execute(new Runnable() {
            @Override
            public void run() {
                for(String uid : userIds) {
                    if(!"".equals(uid)) {
                        WxUser wxUser = userMapper.selectWxUserById(Long.parseLong(uid));
                        TemplateAPI.sendTemplateMsg(userId, message.getTemplateId(), data, wxUser.getOpenId(), (url==null||"".equals(url))?"":url);
                    }
                }
            }
        });
        return AjaxResult.success("发送成功");
    }

    /**
     * 发送文章模板消息
     * @param userId 协会id
     * @param messageConfig 微信模板消息
     * @param wxMessagePress 文章模板消息
     * @return
     */
    public  static AjaxResult sendPressMessage(Long userId,MessageConfig messageConfig ,WxMessagePress wxMessagePress ) {

        String str = null;
        if(messageConfig.getType()==1) {
            str = WxTemplate.getDataWxMessage(
                    wxMessagePress.getFirstData(), wxMessagePress.getFirstColor(),
                    wxMessagePress.getKeyword1Data(), wxMessagePress.getKeyword1Color(),
                    wxMessagePress.getKeyword2Data(), wxMessagePress.getKeyword2Color(),
                    wxMessagePress.getRemark(), wxMessagePress.getRemarkColor());

        }else if(messageConfig.getType()==3){
            str = WxTemplate.getDataWxMessage(
                    wxMessagePress.getFirstData(), wxMessagePress.getFirstColor(),
                    wxMessagePress.getKeyword1Data(), wxMessagePress.getKeyword1Color(),
                    wxMessagePress.getKeyword2Data(), wxMessagePress.getKeyword2Color(),
                    wxMessagePress.getKeyword3Data(), wxMessagePress.getKeyword3Color(),
                    wxMessagePress.getKeyword4Data(), wxMessagePress.getKeyword4Color(),
                    wxMessagePress.getRemark(), wxMessagePress.getRemarkColor());

        }else if(messageConfig.getType()==4) {
            str = WxTemplate.getDataWxMessage(
                    wxMessagePress.getFirstData(), wxMessagePress.getFirstColor(),
                    wxMessagePress.getKeyword1Data(), wxMessagePress.getKeyword1Color(),
                    wxMessagePress.getKeyword2Data(), wxMessagePress.getKeyword2Color(),
                    wxMessagePress.getKeyword3Data(), wxMessagePress.getKeyword3Color(),
                    wxMessagePress.getRemark(), wxMessagePress.getRemarkColor());

        }else if(messageConfig.getType()==5){
            str = WxTemplate.getDataWxMessage(
                    wxMessagePress.getFirstData(), wxMessagePress.getFirstColor(),
                    wxMessagePress.getKeyword1Data(), wxMessagePress.getKeyword1Color(),
                    wxMessagePress.getKeyword2Data(), wxMessagePress.getKeyword2Color(),
                    wxMessagePress.getKeyword3Data(), wxMessagePress.getKeyword3Color(),
                    wxMessagePress.getKeyword4Data(), wxMessagePress.getKeyword4Color(),
                    wxMessagePress.getRemark(), wxMessagePress.getRemarkColor());

        }else if(messageConfig.getType()==6){
            str = WxTemplate.getDataSignUpWxMessage(
                    wxMessagePress.getFirstData(), wxMessagePress.getFirstColor(),
                    wxMessagePress.getKeyword1Data(), wxMessagePress.getKeyword1Color(),
                    wxMessagePress.getKeyword2Data(), wxMessagePress.getKeyword2Color(),
                    wxMessagePress.getKeyword3Data(), wxMessagePress.getKeyword3Color(),
                    wxMessagePress.getKeyword4Data(), wxMessagePress.getKeyword4Color(),
                    wxMessagePress.getRemark(), wxMessagePress.getRemarkColor());

        }
        String data = str;
        String[] userIds = wxMessagePress.getWxUserIds().split(";");
        String url = wxMessagePress.getLinkUrl();

        //创建一个单线程化的线程池，它只会用唯一的工作线程来执行任务，保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
        singleThreadExecutorScreen.execute(new Runnable() {
            @Override
            public void run() {
                for(String uid : userIds) {
                    if(!"".equals(uid)) {
                        WxUser wxUser = userMapper.selectWxUserById(Long.parseLong(uid));
                        TemplateAPI.sendTemplateMsg(userId, wxMessagePress.getTemplateId(), data, wxUser.getOpenId(), (url==null||"".equals(url))?"":url);
                    }
                }
            }
        });
        return AjaxResult.success("发送成功");
    }
}
